<?php
// +----------------------------------------------------------------------
// | XiaMiCMF - 代码如诗
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://shujuwajue.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小虾米 <509129@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class FileManagerTypeController extends AdminBaseController
{
    protected $fileManager_model;
    protected $fileManagerType_model;

    public function _initialize() {
        parent::_initialize();
        $this->fileManager_model = D("FileManager");
        $this->fileManagerType_model = D("FileManagerType");
    }

    /**
     * 文档类型列表
     */
    public function index()
    {
        $list = $this->fileManagerType_model->fetchAll();
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 添加文档类型
     */
    public function add()
    {
        if (IS_POST) {
            $data = $this->fileManagerType_model->create();
            if ($data) {
                $id = $this->fileManagerType_model->add($data);
                if ($id) {
                    $this->success('添加成功', U('index'));
                } else {
                    $this->error('添加失败');
                }
            } else {
                $this->error($this->fileManagerType_model->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 修改文档类型
     */
    public function edit() {
        if (IS_POST) {
            $data = $this->fileManagerType_model->create();
            if ($data) {
                if ($this->fileManagerType_model->save($data) !== false) {
                    $this->success('编辑成功', U('index'));
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($this->fileManagerType_model->getError());
            }
        } else {
            $type_id = I('get.type_id', 0, 'intval');
            $detail = $this->fileManagerType_model->find($type_id);
            $this->assign('detail', $detail);
            $this->display();
        }
    }

    /**
     * 删除文档类型
     */
    public function delete() {
        $type_id = I('post.type_id',0,'intval');
        if (is_numeric($type_id)) {
            if ($this->fileManager_model->where(array('type_id' => $type_id))->count() > 0) {
                $this->error('此类型下包含文件,不能删除,请先删除该类型所有文档');
            }
            $this->fileManagerType_model->delete($type_id);
            $this->success('删除成功', U('index'));
        } else {
            $this->error('请选择要删除的类型');
        }
    }
}